package cn.hy.考试;

import java.util.*;

public class Main02 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = Integer.parseInt(sc.nextLine());
        int[] arr = new int[total];
        for (int i = 0; i < total; i++) {
            arr[i] = Integer.parseInt(sc.nextLine());
        }
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            List<Integer> indexList = map.get(arr[i]);
            if (indexList == null) {
                indexList = new LinkedList<>();
            }
            indexList.add(i);
            map.put(arr[i], indexList);
        }
        int ans = Integer.MIN_VALUE;
        for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
            List<Integer> value = entry.getValue();
            int size = value.size();
            if (size <= 1) {
                continue;
            } else {
                ans = Math.max(value.get(size - 1) - value.get(0), ans);
            }
        }
        if (ans == Integer.MIN_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(ans);
        }
    }
}
